﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrapeCity.PAutoTester.Core;
using System.IO;
using System.Threading;

namespace GrapeCity.PAutoTesterConsole
{
    public class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            // 两个参数1.config 文件地址; 2.CaseList
            CommandLine.WriteCopyright();

            // hidden the console window
            HiddenConsoleWindow();

            CommandLine commandLine = new CommandLine();
            try
            {
                commandLine.Parse(args);
                if (args.Length == 5)
                {
                    System.Console.Error.WriteLine("\r\nParameter error: no inputs specified.\r\n");
                    CommandLineParser.PrintUsage(commandLine, System.Console.Error, System.Console.WindowWidth);
                    return;
                }
                else
                {
                    #region Check parameters
                    // 1. get config file, assembly path in confog file.
                    //Has Config File
                    if (commandLine.HasConfigFile())
                    {
                        if (File.Exists(commandLine.ConfigFile))
                        {
                            Config.Current.Load(commandLine.ConfigFile);
                        }
                        else
                        {
                            throw new FileNotFoundException(string.Format("Cannot find config file \'{0}\'", commandLine.ConfigFile));
                        }
                    }

                    #endregion Check parameters

                    ConsoleTester consoleTester = new ConsoleTester(Config.Current.TestAssembly);
                    consoleTester.ExecuteTest(commandLine);
                }
            }
            catch (CommandLineParserException)
            {
                CommandLineParser.PrintUsage(commandLine, System.Console.Error, System.Console.WindowWidth);
            }
            catch (Exception e)
            {
                Console.WriteLine(Environment.NewLine + e.Message + Environment.NewLine);
            }
        }


        private static void HiddenConsoleWindow()
        {
            // min the consolw window
            Console.Title = "PerformanceTest";
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr et = new IntPtr(0);
            ParenthWnd = Win32API.FindWindow(null, "PerformanceTest");
            //隐藏本dos窗体, 0: 后台执行；1:正常启动；2:最小化到任务栏；3:最大化
            Win32API.ShowWindow(ParenthWnd, 2);
        }
    }
}
